package cn.harry.cabinet.database.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Update
import cn.harry.cabinet.model.entity.OperationLog

/**
 * 操作日志DAO
 */
@Dao
interface OperationLogDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(log: OperationLog): Long

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertAll(logs: List<OperationLog>)

    @Update
    suspend fun update(log: OperationLog)

    @Delete
    suspend fun delete(log: OperationLog)

    @Query("SELECT * FROM operation_logs ORDER BY timestamp DESC")
    suspend fun getAll(): List<OperationLog>

    @Query("SELECT * FROM operation_logs WHERE id = :id")
    suspend fun getById(id: Long): OperationLog?

    @Query("SELECT * FROM operation_logs WHERE operationType = :type ORDER BY timestamp DESC")
    suspend fun getByType(type: String): List<OperationLog>

    @Query("""
        SELECT * FROM operation_logs 
        WHERE operationType = :type AND operationSubType = :subType 
        ORDER BY timestamp DESC
    """)
    suspend fun getByTypeAndSubType(type: String, subType: String): List<OperationLog>

    @Query("SELECT * FROM operation_logs WHERE result = :result ORDER BY timestamp DESC")
    suspend fun getByResult(result: String): List<OperationLog>

    @Query("SELECT * FROM operation_logs WHERE operatorId = :operatorId ORDER BY timestamp DESC")
    suspend fun getByOperator(operatorId: Long): List<OperationLog>

    @Query("""
        SELECT * FROM operation_logs 
        WHERE timestamp BETWEEN :startTime AND :endTime 
        ORDER BY timestamp DESC
    """)
    suspend fun getByTimeRange(startTime: Long, endTime: Long): List<OperationLog>

    @Query("""
        SELECT * FROM operation_logs 
        WHERE targetType = :targetType AND targetId = :targetId 
        ORDER BY timestamp DESC
    """)
    suspend fun getByTarget(targetType: String, targetId: Long): List<OperationLog>

    @Query("SELECT * FROM operation_logs ORDER BY timestamp DESC LIMIT :limit OFFSET :offset")
    suspend fun getPagedLogs(limit: Int, offset: Int): List<OperationLog>

    @Query("SELECT COUNT(*) FROM operation_logs")
    suspend fun getCount(): Int

    @Query("DELETE FROM operation_logs WHERE timestamp < :timestamp")
    suspend fun deleteBeforeTime(timestamp: Long): Int

    @Query("DELETE FROM operation_logs")
    suspend fun deleteAll()

    /**
     * 复合查询：按多个条件筛选
     */
    @Query("""
        SELECT * FROM operation_logs 
        WHERE (:type IS NULL OR operationType = :type)
        AND (:result IS NULL OR result = :result)
        AND (:operatorId IS NULL OR operatorId = :operatorId)
        AND (:startTime IS NULL OR timestamp >= :startTime)
        AND (:endTime IS NULL OR timestamp <= :endTime)
        ORDER BY timestamp DESC
        LIMIT :limit OFFSET :offset
    """)
    suspend fun searchLogs(
        type: String?,
        result: String?,
        operatorId: Long?,
        startTime: Long?,
        endTime: Long?,
        limit: Int,
        offset: Int
    ): List<OperationLog>

    /**
     * 获取最近的N条日志
     */
    @Query("SELECT * FROM operation_logs ORDER BY timestamp DESC LIMIT :limit")
    suspend fun getRecentLogs(limit: Int): List<OperationLog>

    /**
     * 获取今天的日志数量
     */
    @Query("""
        SELECT COUNT(*) FROM operation_logs 
        WHERE timestamp >= :startOfDay AND timestamp < :endOfDay
    """)
    suspend fun getTodayCount(startOfDay: Long, endOfDay: Long): Int

    /**
     * 获取失败的操作数量
     */
    @Query("SELECT COUNT(*) FROM operation_logs WHERE result = 'FAILED'")
    suspend fun getFailedCount(): Int
}
